package com.leyou.item.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 分页查询，以及动态排序
     *
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @Override
    public PageResult<Brand> page(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        PageHelper.startPage(page, rows);

        //创建brand对象用来操作动态sql
        Example example = new Example(Brand.class);

        //判断sortBy不为空，要根据其排序 select xxxx from ABC order by id desc
        if (StringUtils.isNotBlank(sortBy)) {
            example.setOrderByClause(sortBy + (desc ? " DESC" : " ASC"));//这个方法一出现，相当于写好了order by
        }

        if (StringUtils.isNotBlank(key)) {
            Example.Criteria criteria = example.createCriteria();
            criteria.andLike("name", "%" + key + "%");
        }

        //直接查询强转为page就省的使用pageInfo在统计
        Page<Brand> brandPage = (Page<Brand>) brandMapper.selectByExample(example);
        //根据上面查询的结果，封装分页的结果
        return new PageResult<>(brandPage.getTotal(), new Long(brandPage.getPages()), brandPage);
    }

    /**
     * 由于操作多张表，所以需要管理事务
     *
     * @param brand
     * @param cids
     */
    @Transactional
    @Override
    public void addBrand(Brand brand, List<Long> cids) {
        //直接保存品牌，并回显id
        brandMapper.insertSelective(brand);

        //循环保存品牌和分类的关系
        cids.forEach(cid -> {
            brandMapper.insertCategoryBrand(cid, brand.getId());
        });
    }

    /**
     * 修改品牌，涉及到分类的修改，由于之前的分类可能在可能不在，所以直接删除这个品牌对应的所有的分类，重新添加
     *
     * @param brand
     * @param cids
     */
    @Transactional
    @Override
    public void updateBrand(Brand brand, List<Long> cids) {
        //品牌由于有id，可以直接修改
        brandMapper.updateByPrimaryKeySelective(brand);

        //先删除之前品牌对应的所有的分类，然后重新添加
        this.brandMapper.deleteCategory(brand.getId());
        //循环保存品牌和分类的关系
        cids.forEach(cid -> {
            brandMapper.insertCategoryBrand(cid, brand.getId());
        });
    }

    @Override
    public Brand queryBrandById(Long brandId) {

        return brandMapper.selectByPrimaryKey(brandId);
    }

    @Override
    public List<Brand> queryBrandByCategory(Long cid) {



        return brandMapper.queryBrandByCategory(cid);
    }

    @Override
    public List<Brand> queryBrandByIds(List<Long> bids) {


        return brandMapper.selectByIdList(bids);
    }
}
